var _behaviorView = _interopRequireDefault(require("../behavior-view"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _slicedToArray(t, e) {
    return _arrayWithHoles(t) || _iterableToArrayLimit(t, e) || _nonIterableRest();
}

function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
}

function _iterableToArrayLimit(t, e) {
    var r = [], o = !0, i = !1, n = void 0;
    try {
        for (var a, l = t[Symbol.iterator](); !(o = (a = l.next()).done) && (r.push(a.value), 
        !e || r.length !== e); o = !0) ;
    } catch (t) {
        i = !0, n = t;
    } finally {
        try {
            o || null == l.return || l.return();
        } finally {
            if (i) throw n;
        }
    }
    return r;
}

function _arrayWithHoles(t) {
    if (Array.isArray(t)) return t;
}

var position = {
    bottom: function(t, e) {
        var r = _slicedToArray(t, 2), o = r[0], i = r[1];
        return {
            left: o.left + (o.width / 2 - i.width / 2),
            top: o.top + o.height + e
        };
    },
    top: function(t, e) {
        var r = _slicedToArray(t, 2), o = r[0], i = r[1];
        return {
            left: o.left + (o.width / 2 - i.width / 2),
            top: o.top - i.height - e
        };
    },
    "right-top": function(t, e) {
        var r = _slicedToArray(t, 2), o = r[0], i = r[1];
        return {
            left: o.left + (o.width - i.width),
            top: o.top - i.height - e
        };
    },
    left: function(t, e) {
        var r = _slicedToArray(t, 2), o = r[0], i = r[1];
        return {
            left: o.left - i.width - e,
            top: o.top
        };
    },
    right: function(t, e) {
        var r = _slicedToArray(t, 2), o = r[0];
        r[1];
        return {
            left: o.left + o.width + e,
            top: o.top
        };
    }
};

Component({
    behaviors: [ _behaviorView.default ],
    externalClasses: [ "tooltip-arrow-custom", "tooltip-inner-custom" ],
    properties: {
        placement: {
            type: String,
            value: "bottom"
        },
        content: String,
        selector: String,
        padding: {
            type: Number,
            value: 0
        },
        arrowColor: String,
        bg: {
            type: String,
            value: "rgba(70,76,91,.9)"
        },
        custom: {
            type: Boolean,
            value: !1
        },
        showCancel: {
            type: Boolean,
            value: !1
        },
        src: {
            type: String,
            value: "https://7072-production-f72692-1257600323.tcb.qcloud.la/card/fav-tip-close.png"
        }
    },
    data: {
        box: {
            left: 0,
            top: 0
        }
    },
    methods: {
        initialize: function() {
            var r = this, t = this.data, o = t.padding, i = t.placement, n = t.custom;
            setTimeout(function() {
                Promise.all([ r.getOuter(), r.getInner() ]).then(function(t) {
                    var e = {};
                    i.includes("-") && (e.arrow = {
                        left: t[1].width - t[0].width / 2 - 4
                    }), n || (e.box = position[i](t, o)), r.setData(e);
                });
            }, 100);
        },
        getOuter: function() {
            var e = this;
            return new Promise(function(r) {
                var t = e.data.selector;
                t ? wx.createSelectorQuery().select("".concat(t)).boundingClientRect().exec(function(t) {
                    var e = _slicedToArray(t, 1)[0];
                    return r(e);
                }) : r({
                    width: 0,
                    height: 0,
                    left: 0,
                    top: 0
                });
            });
        },
        getInner: function() {
            var t = this;
            return new Promise(function(r) {
                t.createSelectorQuery().select(".tooltip").boundingClientRect().exec(function(t) {
                    var e = _slicedToArray(t, 1)[0];
                    return r(e);
                });
            });
        }
    }
});